/**
 * @author hxp
 * created on 2017/03/01
 */
(function () {
    'use strict';

    angular.module('BlurAdmin.pages')
        .service('AppUtils', AppUtils);

    function AppUtils($window,$uibModal) {

        /*
         普通消息弹出框
         msg
         */
        var tipsHtml = function(msg){
            return '<div class="modal-content"><div class="modal-header"><button type="button" class="close" ng-click="$dismiss()" aria-label="Close"><em class="ion-ios-close-empty sn-link-close"></em></button><h4 class="modal-title" id="myModalLabel">提示</h4></div><div class="modal-body">'+msg+'</div><div class="modal-footer"><button type="button" class="btn btn-primary" ng-click="$dismiss()">确定</button></div></div>';
        }


        var confirmHtml = function(msg){
            return '<div class="modal-content"><div class="modal-header"><button type="button" class="close" ng-click="$dismiss()" aria-label="Close"><em class="ion-ios-close-empty sn-link-close"></em></button><h4 class="modal-title" id="myModalLabel">提示</h4></div><div class="modal-body">'+msg+'</div><div class="modal-footer"><button type="button" class="btn btn-primary" ng-click="$dismiss()">取消</button><button type="button" class="btn btn-primary" ng-click="ok()">确定</button></div></div>';
        }


        return {
            isUndefinedOrNull: function isUndefinedOrNull(obj) {
                return !angular.isDefined(obj) || obj===null;
            },
            goBack: function goBack(){
                $window.history.go(-1);
            },
            jsonToArray: function jsonToArray(obj){
                return obj.reduce(function(memo, obj) {
                    return angular.extend(memo, obj);
                })
            },
            alert: function(msg){
                $uibModal.open({
                    animation: true,
                    template: tipsHtml(msg),
                    size: 'lg',
                    backdrop: 'static'
                });
            },
            confirm: function(msg){
                return $uibModal.open({
                    animation: true,
                    template: confirmHtml(msg),
                    controller: 'ConfirmCtrl',
                    size: 'lg',
                    backdrop: 'static'
                });
            }
        }
    }
})();